package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    EmployeeService employeeService;

    /**
     * @param req
     * @param e
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest req, @RequestBody Employee e) {


        LambdaQueryWrapper<Employee> lqcw = new LambdaQueryWrapper<Employee>();
        String pss = e.getPassword();
        String password = DigestUtils.md5DigestAsHex(pss.getBytes());
        lqcw.eq(Employee::getUsername, e.getUsername());
        Employee employee = employeeService.getOne(lqcw);
        if (employee == null) {
            return R.error("账户不存在");
        }
        if (!employee.getPassword().equals(password)) {
            return R.error("密码错误");
        }
        if (0 == employee.getStatus()) {
            return R.error("该账户已禁用");
        }
        req.getSession().setAttribute("employee", employee.getId());
        return R.success(employee);
    }

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest req) {
        req.getSession().invalidate();
        return R.success("推出成功");
    }

    @PostMapping
    public R<String> save(HttpSession session, @RequestBody Employee e) {
        log.info("新增员工信息{}", e.toString());
        String pass = DigestUtils.md5DigestAsHex("123456".getBytes());
        e.setPassword(pass);

        Long id = (Long) session.getAttribute("employee");


        boolean savaChenk = employeeService.save(e);
        if (savaChenk) {
            return R.success("保存成功");
        }
        return R.error("保存失败");
    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        Page pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(name), Employee::getName, name).orderByDesc(Employee::getUpdateTime);
        employeeService.page(pageInfo, lqw);
        return R.success(pageInfo);
    }

    @PutMapping
    public R<String> update(@RequestBody Employee e) {

        boolean b = employeeService.updateById(e);
        if (b) {
            return R.success("更新成功");
        }
        return R.error("更新失败");
    }

    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id) {
//        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper();
//        lqw.eq(Employee::getId,id);
//        Employee employee = employeeService.getOne(lqw);
        Employee employee = employeeService.getById(id);
        if (employee != null) {

            return R.success(employee);
        }
        return R.error("没有查到改员工信息");
    }
}
